package GUI;

import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.Toolkit;

import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JPanel;

import java.awt.BorderLayout;
import javax.swing.JComboBox;

import logic.Book;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.UIManager;

public class CategorySelector extends JFrame {

	private JPanel parent;
	
	private JCheckBox chkMath;
	private JCheckBox chkArch;
	private JCheckBox chkElec;
	private JCheckBox chkBio;
	private JCheckBox chkBuild;
	private JCheckBox chkTeach;
	private JCheckBox chkSoftware;
	private JCheckBox chkMech;
	private JCheckBox chkIndus;
	private JCheckBox chkFinance;
	private JCheckBox chkHR;
	
	private int type;
	
	public CategorySelector(JPanel parent,ArrayList<String> old,int type) throws HeadlessException {
		getContentPane().setBackground(UIManager.getColor("inactiveCaption"));
		this.parent = parent;
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		this.setSize(485, 362);
		this.setLocation(400, 400);
		this.setAlwaysOnTop(true);
		this.parent.setEnabled(false);
		
		this.type = type;
		
		chkMath = new JCheckBox(Book.CATEGORY_MATH);
		chkMath.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkArch = new JCheckBox(Book.CATEGORY_ARCHITECTURE);
		chkArch.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkElec = new JCheckBox(Book.CATEGORY_ELECTRONICS);
		chkElec.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkBio = new JCheckBox(Book.CATEGORY_BIOTECHNOLOGY);
		chkBio.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkBuild = new JCheckBox(Book.CATEGORY_BUILDING_ENGINEERING);
		chkBuild.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkTeach = new JCheckBox(Book.CATEGORY_TEACHING);
		chkTeach.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkSoftware = new JCheckBox(Book.CATEGORY_SOFTWARE_ENGINEERING);
		chkSoftware.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkMech = new JCheckBox(Book.CATEGORY_MECHANICAL_ENGINEERING);
		chkMech.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkIndus = new JCheckBox(Book.CATEGORY_INDUSTRIAL_ENGINEERING);
		chkIndus.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkFinance = new JCheckBox(Book.CATEGORY_FINANCE);
		chkFinance.setBackground(UIManager.getColor("inactiveCaption"));
		
		chkHR = new JCheckBox(Book.CATEGORY_HUMAN_RESOURCES);
		chkHR.setBackground(UIManager.getColor("inactiveCaption"));
		
		JLabel lblSelectCategories = new JLabel("Select Categories:");
		lblSelectCategories.setFont(new Font("Tahoma", Font.PLAIN, 16));
		
		JButton btnOk = new JButton("OK");
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Validator();
				
			}
		});
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(137)
					.addComponent(lblSelectCategories, GroupLayout.PREFERRED_SIZE, 136, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addComponent(chkMath, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
					.addGap(31)
					.addComponent(chkHR, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addComponent(chkArch, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
					.addGap(31)
					.addComponent(chkFinance, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addComponent(chkElec, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
					.addGap(31)
					.addComponent(chkIndus, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addComponent(chkBio, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
					.addGap(31)
					.addComponent(chkMech, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addComponent(chkBuild, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE)
					.addGap(31)
					.addComponent(chkSoftware, GroupLayout.PREFERRED_SIZE, 159, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(21)
					.addComponent(chkTeach, GroupLayout.PREFERRED_SIZE, 162, GroupLayout.PREFERRED_SIZE))
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(162)
					.addComponent(btnOk, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(11)
					.addComponent(lblSelectCategories, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
					.addGap(7)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(chkMath)
						.addComponent(chkHR))
					.addGap(3)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(chkArch)
						.addComponent(chkFinance))
					.addGap(3)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(chkElec)
						.addComponent(chkIndus))
					.addGap(2)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(chkBio)
						.addComponent(chkMech))
					.addGap(3)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(chkBuild)
						.addComponent(chkSoftware))
					.addGap(3)
					.addComponent(chkTeach)
					.addGap(7)
					.addComponent(btnOk))
		);
		getContentPane().setLayout(groupLayout);
		
		if (old != null) LoadCategories(old);
		this.pack();
		this.setVisible(true);
	}
	
	private void LoadCategories(ArrayList<String> old){
		for(String str : old){
			if (str.equals(chkMath.getText())) chkMath.setSelected(true);
			else if (str.equals(chkArch.getText())) chkArch.setSelected(true);
			else if (str.equals(chkElec.getText())) chkElec.setSelected(true);
			else if (str.equals(chkBio.getText())) chkBio.setSelected(true);
			else if (str.equals(chkBuild.getText())) chkBuild.setSelected(true);
			else if (str.equals(chkTeach.getText())) chkTeach.setSelected(true);
			else if (str.equals(chkSoftware.getText())) chkSoftware.setSelected(true);
			else if (str.equals(chkMech.getText())) chkMech.setSelected(true);
			else if (str.equals(chkIndus.getText())) chkIndus.setSelected(true);
			else if (str.equals(chkFinance.getText())) chkFinance.setSelected(true);
			else if (str.equals(chkHR.getText())) chkHR.setSelected(true);
		}
	}
	
	public void Validator(){
		ArrayList<String> categories = new ArrayList<String>();
		if (chkMath.isSelected()) categories.add(chkMath.getText());
		if (chkArch.isSelected()) categories.add(chkArch.getText());
		if (chkElec.isSelected()) categories.add(chkElec.getText());
		if (chkBio.isSelected()) categories.add(chkBio.getText());
		if (chkBuild.isSelected()) categories.add(chkBuild.getText());
		if (chkTeach.isSelected()) categories.add(chkTeach.getText());
		if (chkSoftware.isSelected()) categories.add(chkSoftware.getText());
		if (chkMech.isSelected()) categories.add(chkMech.getText());
		if (chkIndus.isSelected()) categories.add(chkIndus.getText());
		if (chkFinance.isSelected()) categories.add(chkFinance.getText());
		if (chkHR.isSelected()) categories.add(chkHR.getText());
		
		if(type == 0) ((Add_Book)parent).SetCategories(categories);
		else ((SearchBook)parent).SetCategories(categories);
		this.parent.setEnabled(true);
		this.setVisible(false);
		
	}
}
